package com.jy.adscm.operation.base.controller;


import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.TaskManager;
import com.jy.adscm.operation.base.bean.TaskManagerdetail;
import com.jy.adscm.operation.base.service.TaskManagerService;
import com.jy.adscm.operation.base.service.TaskManagerdetailService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-12-11
 */
@RestController
@RequestMapping("/adscm/operation/taskManagerdetail")
public class TaskManagerdetailController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    TaskManagerdetailService taskManagerdetailService;
    @AdscmLogger("新增任务过程信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody TaskManagerdetail taskManagerdetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getIsNowTime(),null)){
            taskManagerdetail.setIsNowTime("0");
        }
        boolean insert = taskManagerdetailService.insertTaskManagerdetail(taskManagerdetail,token);

        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @AdscmLogger("删除任务过程信息")
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id , HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        TaskManagerdetail taskManagerdetail = taskManagerdetailService.selectById(id);
        if (!taskManagerdetail.getCreator().equals(token.getUser().getId().toString())){
            throw  new AdscmSystemException("不能删除此信息");
         }
        boolean result = taskManagerdetailService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @AdscmLogger("修改任务过程信息")
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody TaskManagerdetail taskManagerdetail, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = taskManagerdetailService.updateTaskManagerdetail(taskManagerdetail,token);

        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectByTid/{taskId}")
    public  SimpleCoreResponse  selectByTid(@PathVariable String taskId) throws  AdscmSystemException{
        List<TaskManagerdetail> TaskManagerdetails = taskManagerdetailService.selectByTid(taskId);
        return  new SimpleCoreResponse("200","查询成功",TaskManagerdetails);
    }
}

